# 建造一个agent，能够理解图片，并且能够总结图片的内容
# 然后根据图片的内容，搜索相关内容，并完成总结
# 最后生成音频进行播放。
# 调度Agent使用模型Deepseek，图片理解和生成音频使用千问模型
# 语音合成模型也是用阿里模型，语音合成CosyVoice大模型-cosyvoice

from agno.agent import Agent, RunResponse
from agno.media import Image
from agno.tools.baidusearch import BaiduSearchTools
from os import getenv
from agno.models.openai.like import OpenAILike
from agno.models.deepseek import DeepSeek
from agno.team.team import Team
from agno.utils.audio import write_audio_to_file

# 编写图片理解agent，使用千问模型qwen-vl-max
image_agent = Agent(
    model=OpenAILike(
        id="qwen-vl-max",
        api_key=getenv("BAILIAN_API_KEY"),
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    ),
    tools=[BaiduSearchTools()],
    markdown=True,
    monitoring=True,
    debug_mode=True,
)

image_story: RunResponse = image_agent.run(
    "告诉我图片内容及最新相关信息",
    images=[Image(
        url="https://i.guancha.cn/news/internet/2025/05/13/20250513092148681.jpg"
        )],
)

print(image_story.content)

# pic_sum_agent.print_response(
#     "告诉我图片内容及最新相关信息",
#     images=[
#         Image(
#             # url="https://i.guancha.cn/news/external/2025/05/14/20250514110031776.jpg"
#             url="https://i.guancha.cn/news/internet/2025/05/13/20250513092148681.jpg"
#             # url="https://i.guancha.cn/news/external/2025/05/14/20250514105910892.jpg"
#         )
#     ],
#     stream=True,
# )


# 语音合成agent，使用语音合成CosyVoice大模型-cosyvoice-v1
audio_agent = Agent(
    model=OpenAILike(
        id="cosyvoice-v1",
        api_key=getenv("BAILIAN_API_KEY"),
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    ),
    markdown=True,
    monitoring=True,
    debug_mode=True,
)


pic_sum_audie_team = Team(
    name="图片理解语音合成团队",
    mode="coordinate",
    members=[image_agent, audio_agent],
    description="这是一个图片理解语音合成团队，你的任务是理解图片内容，并根据图片内容搜索相关信息，并生成音频。",
    instructions=[
        "首先,识别图片内容，并根据图片内容搜索相关信息。",
        "然后根据图片内容生成音频。"
    ],
    monitoring=True,
    debug_mode=True,
)

audio_story: RunResponse = audio_agent.run(
    f"讲述这张图片的内容: {image_story.content}"
)

if audio_story.response_audio is not None:
    write_audio_to_file(
        audio=audio_story.response_audio.content, filename="sample_story.wav"
    )

if __name__ == "__main__":
    pic_sum_audie_team.run()